package com.mvc.framework.util;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.extremecomponents.table.context.Context;
import org.extremecomponents.table.context.HttpServletRequestContext;
import org.extremecomponents.table.core.TableConstants;
import org.extremecomponents.table.limit.Limit;
import org.extremecomponents.table.limit.LimitFactory;
import org.extremecomponents.table.limit.Sort;
import org.extremecomponents.table.limit.TableLimit;
import org.extremecomponents.table.limit.TableLimitFactory;

public final class PageUtils {
	public static final String PREFIX_WITH_TABLEID = "ec_";
	public static final int PAGE_SIZE_ALL = -999;
	private static final String REQUEST_PAGE_SIZE = PREFIX_WITH_TABLEID + TableConstants.CURRENT_ROWS_DISPLAYED;
	private static final String SESSION_PAGE_SIZE = "s_" + REQUEST_PAGE_SIZE;

	private PageUtils() {
	}

	public static PageSearch getPageSearch(HttpServletRequest request, Limit limit) {
		PageSearch page = new PageSearch();
		if (request != null) {
			int pageSize = getSessionPageSize(request);
			boolean isExported = PageUtils.isExported(request);
			int requestPageSize=0;
			if(isExported){
				requestPageSize = setExportPageSize(request, requestPageSize);
			}
			if(requestPageSize==0){
				requestPageSize = getRequestPageSize(request);
			}
			if(requestPageSize!=pageSize){
				pageSize = requestPageSize;
				if(!isExported&&pageSize!=0){
					request.getSession().setAttribute(SESSION_PAGE_SIZE, pageSize);
				}
			}
			pageSize = (pageSize == PAGE_SIZE_ALL)? 0 : pageSize;
			if (pageSize != 0) {
				page.setPageSize(pageSize);
			}
			String requestCurrentPage = request.getParameter(PREFIX_WITH_TABLEID + TableConstants.PAGE);
			int cureentPage = 1;
			if (!StringUtils.isEmpty(requestCurrentPage)) {
				cureentPage = Integer.parseInt(requestCurrentPage);
			}
			page.setCurrentPage(cureentPage);
			Sort sort = limit.getSort();
			//Only support single property 
			if (sort != null && sort.getProperty() != null && sort.getSortOrder() != null) {
				page.setSortProperty(sort.getProperty());
				page.setSortOrder(sort.getSortOrder());
			}
		}
		return page;
	}

	protected static int getRequestPageSize(HttpServletRequest request) {
		int requestPageSize = 0;
	    try{
	    	requestPageSize = Integer.parseInt(request.getParameter(REQUEST_PAGE_SIZE));
	    }catch (NumberFormatException e) {
	    }
	    return requestPageSize;
    }

	protected static int setExportPageSize(HttpServletRequest request, int requestPageSize) {
	    String isALl = request.getParameter(TableConstants.EXTREME_COMPONENTS_INSTANCE);
	    try{
	    	requestPageSize = Integer.parseInt(isALl);
	    	if(requestPageSize==PAGE_SIZE_ALL){
	    		requestPageSize = Integer.MAX_VALUE;
	    	}
	    }catch (Exception e) {
	    }
	    return requestPageSize;
    }

	protected static int getSessionPageSize(HttpServletRequest request) {
	    Object sessionPageSize = request.getSession().getAttribute(SESSION_PAGE_SIZE);
	    int pageSize = 0;
	    if(sessionPageSize!=null){
	    	pageSize = (Integer)sessionPageSize;
	    }
	    return pageSize;
    }

	public static void setPageParameter(HttpServletRequest request,PageSearch page){
		request.setAttribute(PREFIX_WITH_TABLEID + TableConstants.CURRENT_ROWS_DISPLAYED,page.getPageSize());
		request.setAttribute(PREFIX_WITH_TABLEID + TableConstants.PAGE,page.getCurrentPage());
	}
	public static Limit getLimit(HttpServletRequest request) {
		Context context = new HttpServletRequestContext(request);
		LimitFactory limitFactory = new TableLimitFactory(context);
		TableLimit limit = new TableLimit(limitFactory);
		return limit;
	}
	
	public static boolean isExported(HttpServletRequest request){
		String isExported = request.getParameter(TableConstants.EXPORT_TABLE_ID);
		return StringUtils.isNotBlank(isExported);
	}

}
